﻿using MVC_Project.BLL;
using Project.DAL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Project.BLL
{
    public class CartDetailBll : BaseBll<milktea_cart_detail>
    {
        CartDetailDal dal = new CartDetailDal();

        public override void SetDal()
        {
            Dal = new CartDetailDal();
        }

        public void CartJiaGoods(milktea_cart_detail detail)
        {
            dal.CartJiaGoods(detail);

        }

        public bool CartDetailAdd(milktea_cart_detail detail) { 
            return dal.CartDetailAdd(detail);
        }

        public int ShuLian(int CId)
        {
            var list = Dal.Query();

            int count = list.Where(d => d.Cart_id == CId).Sum(d => d.quantity);

            return count;
        }

        public List<milktea_cart_detail> details(int id) { 
            var list = Dal.Query().Where(d => d.Cart_id == id);

            return list.ToList();
        }

        public bool UpdateSL(int sl, int cid,int gid) {
            var data = Dal.Query().Where(d => d.Cart_id == cid && d.Goods_Id == gid).FirstOrDefault();

            if (sl > data.quantity)
            {
                dal.JiaCart(cid, sl - data.quantity, data.price / data.quantity);

                data.price = (data.price / data.quantity) * sl;

                data.quantity = sl;

                Dal.Update(data);

                return Dal.SaveChanges();
            }
            else if (sl < data.quantity && sl != 0 )
            {
                dal.JianCart(cid, data.quantity-sl, data.price / data.quantity);

                data.price = (data.price / data.quantity) * sl;

                data.quantity = sl;

                Dal.Update(data);

                return Dal.SaveChanges();
            }
            else {
                dal.JianCart(cid, data.quantity, data.price);

                Dal.Delete(data);

                return Dal.SaveChanges();
            }
        }
    }
}
